package net.chrone.quickpayapi.service.impl;


import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.chrone.quickpayapi.mapper.PayforTxnMapper;
import net.chrone.quickpayapi.model.Merchant;
import net.chrone.quickpayapi.model.Order;
import net.chrone.quickpayapi.model.PayforTxn;
import net.chrone.quickpayapi.model.PayforTxnExample;
import net.chrone.quickpayapi.model.PayforTxnExample.Criteria;
import net.chrone.quickpayapi.service.MerchantService;
import net.chrone.quickpayapi.service.PayforService;
import net.chrone.quickpayapi.service.RedisService;
import net.chrone.quickpayapi.util.Constant;
@Service
public class PayforServiceImpl implements PayforService {
	
	private  final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private PayforTxnMapper payforTxnMapper;
	@Autowired
	private MerchantService merchantService;
	@Autowired
	private RedisService redisService;
	
	@Override
	public PayforTxn findByOrgOrderNo(String orgId, String orderNo) {
		PayforTxnExample example = new PayforTxnExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrgIdEqualTo(orgId);
		criteria.andOrderNoEqualTo(orderNo);
		List<PayforTxn> txns = payforTxnMapper.selectByExample(example);
		if(txns!=null&&txns.size()>0){
			return txns.get(0);
		}
		return null;
	}


	@Override
	public PayforTxn findByOrgSsn(String orgId, String ssn) {
		PayforTxnExample example = new PayforTxnExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrgIdEqualTo(orgId);
		criteria.andOrgPayforSsnEqualTo(ssn);
		List<PayforTxn> txns = payforTxnMapper.selectByExample(example);
		if(txns!=null&&txns.size()>0){
			return txns.get(0);
		}
		return null;
	}

	@Override
	public PayforTxn newOrder(PayforTxn payforTxn, Order order) {
		Merchant merchant = merchantService.find(order.getOrgId(), order.getAccount());
		DateTime dateTime = new DateTime();
		String txnId = dateTime.getMillis()+redisService.getIdFromRedis("payforId");
		payforTxn.setTxnId(txnId);
		payforTxn.setTxnDt(dateTime.toString("yyyyMMdd"));
		payforTxn.setAccount(order.getAccount());
		payforTxn.setCreated(new Date());
		payforTxn.setAccountName(merchant.getRealName());
		payforTxn.setCardNo(merchant.getCardNo());
		payforTxn.setMobile(merchant.getMobile());
		payforTxn.setCityCd(merchant.getCityCd());
		payforTxn.setBankCd(redisService.findPmsBank(merchant.getPmsBankNo()).getBankNo());
		payforTxn.setPmsBankCd(merchant.getPmsBankNo());
		payforTxn.setPayChannelId(order.getPayChannelId());
		payforTxn.setOrderNo(order.getOrderDt());
		payforTxn.setCreated(dateTime.toDate());
		int rows = payforTxnMapper.insertSelective(payforTxn);
		if(1==rows){
			return payforTxn;
		}
		return null;
	}


	@Override
	public int updateByPrimaryKey(PayforTxn payforTxn) {
		return payforTxnMapper.updateByPrimaryKeySelective(payforTxn);
	}


	@Override
	public PayforTxn findByPrimaryKey(String orderId) {
		return payforTxnMapper.selectByPrimaryKey(orderId);
	}

	@Override
	public List<PayforTxn> find(String payChannelId, int status) {
		PayforTxnExample example = new PayforTxnExample();
		Criteria criteria = example.createCriteria();
		criteria.andPayChannelIdEqualTo(payChannelId);
		criteria.andPayStEqualTo(status);
		return payforTxnMapper.selectByExample(example);
	}


	@Override
	public PayforTxn getByOrder(String orderNo) {
		PayforTxnExample example = new PayforTxnExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrderNoEqualTo(orderNo);
		List<PayforTxn> payforTxns = payforTxnMapper.selectByExample(example);
		if(payforTxns!=null&&payforTxns.size()>0){
			return payforTxns.get(0);
		}
		return null;
	}

	//插入自动结算通道付款交易
	@Override
	public void newFromOrder(Order order,int paySt,String respCode,String respMsg) {
		Merchant merchant = merchantService.find(order.getOrgId(), order.getAccount());
		DateTime dateTime = new DateTime();
		String txnId = dateTime.getMillis()+redisService.getIdFromRedis("payforId");
		PayforTxn payforTxn = new PayforTxn();
		payforTxn.setTxnId(txnId);
		payforTxn.setTxnDt(dateTime.toString("yyyyMMdd"));
		payforTxn.setOrgId(order.getOrgId());
		payforTxn.setAccount(order.getAccount());
		payforTxn.setAccountName(merchant.getRealName());
		payforTxn.setCardNo(merchant.getCardNo());
		payforTxn.setCertNo(merchant.getCertNo());
		payforTxn.setMobile(merchant.getMobile());
		payforTxn.setCityCd(merchant.getCityCd());
		payforTxn.setBankCd(redisService.findPmsBank(merchant.getPmsBankNo()).getBankNo());
		payforTxn.setPmsBankCd(merchant.getPmsBankNo());
		payforTxn.setAmount(order.getSettleAmt());//清算金额
		payforTxn.setPayChannelId(order.getPayChannelId());
		payforTxn.setNotifyUrl(order.getPayforNotifyUrl());
		payforTxn.setOrderNo(order.getOrderNo());
		payforTxn.setOrderDt(order.getOrderDt());
		payforTxn.setOrgPayforSsn(txnId);
		payforTxn.setCreated(dateTime.toDate());
		payforTxn.setPaySt(paySt);
		if(StringUtils.isNotEmpty(respCode)){
			payforTxn.setRespCode(respCode);
		}
		if(StringUtils.isNotEmpty(respMsg)){
			payforTxn.setRespDesc(respMsg);
		}
		int rows = payforTxnMapper.insertSelective(payforTxn);
		if(1==rows){
			logger.debug("插入付款交易成功,txn_id="+txnId);
		}
	}

	/**
	 * 按通道获取待付款交易
	 * @param channel:通道ID
	 * @param deleyTime:延迟时间,以分钟为单位
	 */
	@Override
	public List<PayforTxn> findAllNewPayTxn(String channel,int deleyTime) {
		DateTime dateTime = new DateTime();
		PayforTxnExample example = new PayforTxnExample();
		Criteria criteria = example.createCriteria();
		criteria.andPayStEqualTo(Constant.PAYFOR_ST_NEW);
		criteria.andPayChannelIdEqualTo(channel);
		criteria.andCreatedLessThan(dateTime.minusMinutes(deleyTime).toDate());
		return payforTxnMapper.selectByExample(example);
	}



}
